package com.jiaozi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jiaozi.base.enums.sys.StateEnum;
import com.jiaozi.base.utils.bean.R;
import com.jiaozi.system.entity.SysRole;
import com.jiaozi.system.mapper.SysPostMapper;
import com.jiaozi.system.entity.SysPost;
import com.jiaozi.system.pojo.param.sys.post.PostListParam;
import com.jiaozi.system.pojo.param.sys.post.PostSaveUpdateParam;
import com.jiaozi.system.pojo.vo.sys.post.SysPostListVO;
import com.jiaozi.system.service.SysPostService;
import org.springframework.stereotype.Service;

/**
 * 系统-岗位(SysPost)表服务实现类
 *
 * @author 张崇阳
 * @since 2024-10-31 10:45:11
 */
@Service("sysPostService")
public class SysPostServiceImpl extends MPJBaseServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    @Override
    public R<IPage<SysPostListVO>> postPage(Long pageNum, Long pageSize, PostListParam param) {
        Page page = new Page(pageNum, pageSize);
        MPJLambdaWrapper<SysPost> sysPostMPJLambdaWrapper=new MPJLambdaWrapper<>();
        sysPostMPJLambdaWrapper.like(StrUtil.isNotBlank(param.getPostName()), SysPost::getPostName, param.getPostName());
        sysPostMPJLambdaWrapper.like(StrUtil.isNotBlank(param.getPostCode()), SysPost::getPostCode, param.getPostCode());
        sysPostMPJLambdaWrapper.eq(param.getPostState()!=null, SysPost::getPostState, param.getPostState());
        IPage<SysPostListVO>iPage=  this.selectJoinListPage(page,SysPostListVO.class,sysPostMPJLambdaWrapper);
        return R.ok(iPage);
    }

    @Override
    public R<SysPostListVO> postInfo(String postId) {
        SysPostListVO sysPostListVO =BeanUtil.copyProperties(this.getById(postId), SysPostListVO.class);
        return R.ok(sysPostListVO);
    }

    @Override
    public R savePost(PostSaveUpdateParam param) {
        SysPost sysPost= BeanUtil.copyProperties(param, SysPost.class);
        this.save(sysPost);
        return R.ok();
    }

    @Override
    public R updatePost(String postId, PostSaveUpdateParam param) {
        LambdaUpdateWrapper<SysPost> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(SysPost::getPostSort,param.getPostSort());
//        lambdaUpdateWrapper.set(SysPost::getPostState,param.getPostState());
        lambdaUpdateWrapper.set(SysPost::getPostName,param.getPostName());
        lambdaUpdateWrapper.set(SysPost::getPostCode,param.getPostCode());
        lambdaUpdateWrapper.eq(SysPost::getId,postId);
        this.update(lambdaUpdateWrapper);
        return R.ok();
    }

    @Override
    public R deletePost(String postId) {
        this.removeById(postId);
        return R.ok();
    }

    @Override
    public R updatePostStatus(String postId) {
        SysPost sysPost=this.getById(postId);
        LambdaUpdateWrapper<SysPost> sysPostLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        if(sysPost.getPostState()== StateEnum.NORMAL){
            sysPostLambdaUpdateWrapper.set(SysPost::getPostState,StateEnum.STOP);
        }else{
            sysPostLambdaUpdateWrapper.set(SysPost::getPostState,StateEnum.NORMAL);
        }
        sysPostLambdaUpdateWrapper.eq(SysPost::getId,postId);
        this.update(sysPostLambdaUpdateWrapper);
        return R.ok();
    }
}

